/* Hybrid System Abstractor
 *
 * Copyright (c) 2013, Fortiss GmbH - An-Institut TU Muenchen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fortiss.util;

import javax.xml.bind.annotation.*;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.io.Serializable;



@XmlRootElement
public class Interval implements Serializable
{
    @XmlAttribute
    private double lower;
    @XmlAttribute
    private double upper;

    public Interval()   {}
    
    public Interval(double l, double u)
    {
        lower = l;
        upper = u;
    }

    public Interval(Interval i)
    {
        lower = i.lower;
        upper = i.upper;
    }

    public double getLower() { return lower; }
    public double getUpper() { return upper; }
    
    public Interval times(double s)
    {
        if (s>=0)
            return new Interval(lower*s, upper*s);
        else
            return new Interval(upper*s, lower*s);
    }

    public Interval times(Interval i)
    {
        List<Double> vals = Arrays.asList(new Double[]{
            this.lower* i.lower,
            this.lower* i.upper,
            this.upper* i.lower,
            this.upper* i.upper
        });
        return new Interval(Collections.min(vals), Collections.max(vals));
    }

    public Interval plus(Interval i)
    {
        return new Interval(this.lower + i.lower, this.upper + i.upper);
    }

    public void print()
    {
        System.out.format("(%.8f, %.8f) ", lower, upper);
    }
    
}
